package com.zhou.web;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class userServlet extends HttpServlet {
    String encoding = null;
    @Override
    public void init(ServletConfig config) throws ServletException {
       encoding = config.getInitParameter("encoding");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        System.out.println(name);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //session
        HttpSession session = req.getSession();
        req.setCharacterEncoding(encoding);
        String name = req.getParameter("username");
        System.out.println(name);
        String[] hobbies = req.getParameterValues("hobby");
        for (String hobby : hobbies) {
            System.out.println(hobby);
        }
        req.setAttribute("username",name);
//        session.setAttribute("username",name);
        //转发
  //      req.getRequestDispatcher("/success.jsp").forward(req,resp);
        resp.sendRedirect("/success.jsp");
//        resp.addCookie();//设置cookie值
//        PrintWriter writer = resp.getWriter();//得到一个可以给前端的输出流对象
    }
}
